package com.yycwxt.modules.admin.operatefee;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.yycwxt.common.BaseService;
import com.yycwxt.domain.OperateFee;
import lombok.SneakyThrows;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class OperateFeeService extends BaseService<OperateFeeMapper, OperateFee> {

    private OperateFeeMapper operateFeeMapper;


    public OperateFeeService(OperateFeeMapper operateFeeMapper){
        this.operateFeeMapper = operateFeeMapper;
    }

    @SneakyThrows
    public boolean importFileToDB(boolean deleteHistory, MultipartFile file) {
        if (deleteHistory) {
            this.remove(null);
        }
        EasyExcel.read(file.getInputStream(), OperateFee.class, new ReadListener<OperateFee>() {

            private static final int BATCH_SIZE = 1000;
            private List<OperateFee> ls = Lists.newArrayList();

            @Override
            public void invoke(OperateFee data, AnalysisContext context) {
                //System.err.println(data);
                ls.add(data);
                if (ls.size() >= BATCH_SIZE) {
                    OperateFeeService.this.saveBatch(ls);
                    ls.clear();
                }
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
                OperateFeeService.this.saveBatch(ls);
            }
        }).sheet().doRead();
        return true;
    }

    public List<Map<String, Object>> selectBcf(int i) {
        Map<String, Object> map = new HashMap<>();
        map.put("i",202208);
        return operateFeeMapper.selectBcf(map);
    }
}
